package org.dlsu.mt.parser.generation;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class GrammarLoader
{
	BufferedReader br;
	
	public GrammarLoader(String filename)
	{
		try
		{
			br = new BufferedReader(new FileReader(filename));
		}
		catch(FileNotFoundException e)
		{
			//System.out.println("Error! File Not Found");
			br = null;
		}
	}
	
	public GrammarList load() throws IOException
	{
		if(br!=null)
		{
			GrammarList list = new GrammarList();
			String temp = null;
			boolean foundStart = false;
			while((temp = br.readLine()) != null)
			{
				if(!foundStart||temp.equals("")||temp.equals("**END**"))
				{
					if(!foundStart&&temp.equals("Rules:"))
						foundStart = true;
				}
				else
				{
					String[] split = temp.split(" = ", 2);
					list.addItem(split[0], split[1]);
				}
			}
			return list;
		}
		else
			return null;
	}
}